package com.zhenai.rocyuan.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhenai.rocyuan.domain.Pager;
import com.zhenai.rocyuan.domain.User;
import com.zhenai.rocyuan.service.mail.MailSendService;
import com.zhenai.rocyuan.service.user.UserService;


@Controller
@RequestMapping("/user")
public class UserController {
	   @Autowired
	    private UserService service;
	   @Autowired
	    private MailSendService mailSendService;
	    
	   /**
	    * 显示添加页
	    * @param model
	    * @return
	    */
	    @RequestMapping("/add")
	    public String add(Model model) {
	    	
	    	Map<String,Object> root = new HashMap<String,Object>();
			root.put("username", "rocyuan");	
			root.put("content","Welcome to the real world ");
			boolean success = mailSendService.sendTemplateMail(root, "1045138965@qq.com","主题标题","mail4.ftl");
			model.addAttribute("success", success);
			model.addAttribute("user", new User());
	        return "/update.jsp";
	    }
	    
	    /**
	     * 保存数据操作
	     * @param u
	     * @return
	     */
	    @RequestMapping("/update")
	    public String update(User u) {
	       
	    	if(u.getId()>0){
	    		//更新
	    	service.update(u);
	    	}else{
	    		//插入
	        service.insert(u);
	    	}
	        return "redirect:/user/list.do";
	    }
	    
	    /**
	     * 添加100000条记录测试
	     * @return
	     */
	    @RequestMapping("/test")
	    public String test() {
	    	long t=new Date().getTime();
	    	for (int i=89000;i<89100;i++){
	    	User u=new User();
	         u.setUsername("test"+i);
	         u.setPassword("password"+i);
	         u.setEmail("aaa@com.cn");
	         int roleid=i%2;
	         u.setRoleId(roleid);
	        service.insert(u);
	    	}
	    	long time=new Date().getTime()-t;
	    	System.out.println("添加100000条记录，花费时间:"+time+"ms，平均每条"+time/100000+"ms");
	        return "redirect:/user/list.do";
	    }
	    
	    /**
	     * 显示修改页面
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequestMapping("/edit")
	    public String get(Integer id, Model model) {
	      
	        model.addAttribute("user", service.getUserById(id));
	        return "/update.jsp";
	    }
	    
	    /**
	     * 删除操作
	     * @param id
	     * @return
	     */
	    @RequestMapping("/del")
	    public String del(Integer id) {
	       service.delete(id);
	       return "redirect:/user/list.do";
	    }
	    
	    /**
	     * 显示分页数据
	     * @param request
	     * @param model
	     * @return
	     */
	    @RequestMapping("/list")
	    public String index(HttpServletRequest request,   Model model) {
	    	int pageSize=20;
	    	int page = request.getParameter("page") == null ? 1 : Integer.valueOf(request.getParameter("page"));
			if (page < 1)	page = 1;
	    	Pager p=service.getList(page, pageSize);
	    	
	    	model.addAttribute("pager",p.getPagerStr());
	        model.addAttribute("list", p.getItems());
	        return "/list.jsp";
	    }
	    
	
	    /*******************************    ftl  test                  ************************************/
	    @RequestMapping("/hello")
	    public String sayHello(ModelMap map){
	    	map.put("name", "hello ftl workld!");
	    	
			return "/hello.ftl";
	    	
	    }

}
